package cm.com.carlamanda.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cm.com.carlamanda.constant.Regex;
import cm.com.carlamanda.domain.InputParam;
import cm.com.carlamanda.domain.Interface;
import cm.com.carlamanda.domain.Page;
import cm.com.carlamanda.domain.UserInfo;
import cm.com.carlamanda.dto.InterfaceDto;
import cm.com.carlamanda.service.HttpService;
import cm.com.carlamanda.service.InputParamService;
import cm.com.carlamanda.service.InterfaceService;
import cm.com.carlamanda.util.StringUtil;

import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping(value="interface")
public class InterfaceController {
    @Autowired
    private InterfaceService interfaceService;
    @Autowired
    private HttpService httpService;
    @Autowired
    private InputParamService inputParamService;
    
    @RequestMapping(value="search",method=RequestMethod.GET)
    @ResponseBody
    public JSONObject getAllInterface(@RequestParam(value="name",required=false) String name,@RequestParam(value="userEname",required=false) String userEname,
            @RequestParam(value="page",defaultValue="1",required=false) int page,@RequestParam(value="pageSize",defaultValue="10",required=false) int pageSize,String sortColumn,String sortAsc){
        JSONObject jsonObject = new JSONObject();
        InterfaceDto inter =new InterfaceDto();
        inter.setName(name);
        UserInfo user =new UserInfo();
        user.setUserEname(userEname);
        inter.setUser(user);
        Page pa=new Page();
        pa.setPage(page);
        pa.setPageSize(pageSize);
        if(StringUtil.isNotEmpty(sortColumn)){
            pa.setSortColumn(sortColumn);
            pa.setSortAsc("desc".equalsIgnoreCase(sortAsc)?"desc":"asc");
        }
        List<InterfaceDto> list = interfaceService.search(inter,pa);
        jsonObject.put("list",list);
        jsonObject.put("result",true);
        jsonObject.put("page", pa);
        return jsonObject;
    }
    
    @RequestMapping(value="add",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject add(@RequestBody InterfaceDto inter,HttpSession session){
        //TODO convert inputJson
        JSONObject jsonObject = new JSONObject();
        UserInfo userInfo=(UserInfo) session.getAttribute("userGroupInfo");
        if(userInfo!=null){
            inter.setCreatorId(userInfo.getUuId());
        }
        if(StringUtil.isNotEmpty(inter.getInputJson()) && (inter.getParams()==null ||inter.getParams().size()==0)){
            inter.convertToParams();
        }
        interfaceService.add(inter);
        if(inter!=null&&StringUtil.isNotEmpty(inter.getModuleId())){
            interfaceService.linkModule(inter.getModuleId(), inter.getId());
        }
        if(inter!=null &&inter.getParams()!=null &&inter.getParams().size()>0){
            for(InputParam param:inter.getParams()){
                param.setId(UUID.randomUUID().toString());
            }
            interfaceService.batchAddParam(inter);
        }
        jsonObject.put("result",true);
        return jsonObject;
    }
    @RequestMapping(value="del",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject del(@RequestParam("id") String id){
        JSONObject jsonObject = new JSONObject();
            interfaceService.del(id);
            jsonObject.put("result",true);
        return jsonObject;
    }
    
    @RequestMapping(value="edit",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject edit(@RequestBody Interface inter){
        JSONObject jsonObject = new JSONObject();
        interfaceService.edit(inter);
        jsonObject.put("result",true);
        return jsonObject;
    }
    
    @RequestMapping(value="get",method=RequestMethod.GET)
    @ResponseBody
    public JSONObject get(@RequestParam("id") String id){
        JSONObject jsonObject = new JSONObject();
        InterfaceDto result=interfaceService.get(id);
        List<InputParam> params=inputParamService.getParamListByInterfaceId(id);
        result.setParams(params);
        jsonObject.put("data",result);
        jsonObject.put("result",true);
        return jsonObject;
    }
    
    @RequestMapping(value="load",method=RequestMethod.GET)
    @ResponseBody
    public JSONObject loadUrl(String url){
        JSONObject jsonObject = new JSONObject();
        try {
            List<String> jsLinks=httpService.getJSLinks(url);
            for(String jsUrl:jsLinks){
                String jsContent =httpService.getInputContent(jsUrl);
                List<String> urls=StringUtil.regexString(jsContent, Regex.RGX_AJAX_URL);
                for(String interfaceUrl:urls){
                    
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject.put("result",true);
        return jsonObject;
    }
    @RequestMapping(value="debug",method=RequestMethod.GET)
    @ResponseBody
    public JSONObject debugUrlByGET(@RequestBody Interface inter,HttpSession session){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",true);
        return jsonObject;
    }
}
